import { type CrudConfig } from '@/components';
import urlConstants from '@/constants/urlConstants';
import type { ProColumns } from '@ant-design/pro-components';

// 排序规则表格数据类型
export interface SortRuleItem {
  id?: string;
  priority: number;
  fieldName: string;
  sortOrder: 'asc' | 'desc';
}

// 排序策略数据类型
export interface SortSettingRecord {
  paramIndex?: number;
  templateName: string;
  positionName: string;
  description?: string;
  sortRules?: SortRuleItem[];
  createTime?: string;
  updateTime?: string;
  updateUser?: string;
}

// 统一的页面配置函数 - 接收 translate 函数作为参数
export const getPageConfig = (translate: (prefix: string, key: string) => string) => {
  const t = (key: string) => translate('strategyManage', key);
  const tCommon = (key: string) => translate('common', key);

  return {
    // CRUD配置
    crudConfig: {
      endpoints: {
        list: urlConstants.ADUIT_MANAGE.LIST,
        create: urlConstants.ADUIT_MANAGE.CREATE,
        update: urlConstants.ADUIT_MANAGE.EDIT,
        delete: urlConstants.ADUIT_MANAGE.DELETE,
        detail: urlConstants.ADUIT_MANAGE.LIST,
      },
    } as CrudConfig,

    // 搜索字段配置
    searchFields: [
      {
        name: 'templateName',
        label: t('templateName'),
        type: 'input' as const,
      },
      {
        name: 'positionName',
        label: t('positionName'),
        type: 'input' as const,
      },
    ],

    // 表格列配置
    columns: [
      {
        title: tCommon('paramIndex'),
        dataIndex: 'paramIndex',
        key: 'paramIndex',
        width: 80,
        fixed: 'left',
      },
      {
        title: t('templateName'),
        dataIndex: 'templateName',
        key: 'templateName',
        width: 150,
      },
      {
        title: t('positionName'),
        dataIndex: 'positionName',
        key: 'positionName',
        width: 150,
      },
      {
        title: t('description'),
        dataIndex: 'description',
        key: 'description',
        width: 200,
        ellipsis: true,
      },
      {
        title: tCommon('updateUser'),
        dataIndex: 'updateUser',
        key: 'updateUser',
        width: 150,
      },
      {
        title: tCommon('updateTime'),
        dataIndex: 'updateTime',
        key: 'updateTime',
        width: 160,
        valueType: 'dateTime',
      },
    ] as ProColumns<SortSettingRecord>[],

    // 表单分组配置
    formSections: [
      // 基本信息分组
      {
        title: tCommon('baseInfo'),
        fields: [
          {
            name: 'templateName',
            label: t('templateName'),
            type: 'select' as const,
            rules: [{ required: true, message: t('templateNameRequired') }],
            fieldProps: {
              placeholder: tCommon('selectPlaceholder'),
            },
          },
          {
            name: 'positionName',
            label: t('positionName'),
            type: 'input' as const,
            rules: [{ required: true, message: t('positionNameRequired') }],
            fieldProps: {
              placeholder: tCommon('inputPlaceholder'),
            },
          },
          {
            name: 'description',
            label: t('description'),
            type: 'textarea' as const,
            span: 24,
            fieldProps: {
              placeholder: tCommon('inputPlaceholder'),
              rows: 3,
            },
          },
        ],
      },
      // 排序管理分组
      {
        title: t('sortManagement'),
        fields: [
          {
            name: 'sortRules',
            label: '', // 隐藏表格的默认标签，因为我们使用section标题
            type: 'editTable' as const,
            span: 24,
            editTableConfig: {
              columns: [
                {
                  title: t('priority'),
                  dataIndex: 'priority',
                  type: 'number' as const,
                  width: 120,
                  required: true,
                  fieldProps: {
                    min: 1,
                    precision: 0,
                  },
                },
                {
                  title: t('fieldName'),
                  dataIndex: 'fieldName',
                  type: 'input' as const,
                  width: 200,
                  required: true,
                },
                {
                  title: t('sortOrder'),
                  dataIndex: 'sortOrder',
                  type: 'select' as const,
                  width: 150,
                  fieldProps: {
                    options: [
                      { label: t('asc'), value: 'asc' },
                      { label: t('desc'), value: 'desc' },
                    ],
                  },
                },
              ],
              rowKey: 'id',
              maxRows: 20,
              minRows: 0,
              addButtonText: t('addSortRule'),
            },
          },
        ],
      },
    ],

    // 字典枚举配置
    dictEnum: {},
  };
};
